package com.ruoyi.pcj.interfaces.app;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pcj.domain.PcjChatGroup;
import com.ruoyi.pcj.domain.PcjChatGroupType;
import com.ruoyi.pcj.service.IPcjChatGroupService;
import com.ruoyi.pcj.service.IPcjChatGroupTypeService;
import com.ruoyi.pcj.service.IPcjChatGroupUserService;
import com.ruoyi.pcj.service.IPcjDefConfigService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 聊天模块相关信息接口
 */
@AllArgsConstructor
@RestController
@RequestMapping("/wx/chat")
public class ChatController {

    private IPcjChatGroupTypeService chatGroupTypeService;

    private IPcjChatGroupService chatGroupService;

    private IPcjDefConfigService pcjDefFileConfigService;

    private IPcjChatGroupUserService chatGroupUserService;

    /**
     * 获取聊天室类型
     *
     * @return
     */
    @PostMapping("/getChatGroupTypeData")
    public AjaxResult getChatGroupTypeData() {
        List<PcjChatGroupType> list = chatGroupTypeService.getChatGroupTypeData();
        return AjaxResult.ok(list);
    }

    /**
     * 获取当前未结束的聊天室
     *
     * @param params {"groupType":"聊天室类型id"}
     * @return
     */
    @PostMapping("/getChatGroupData")
    public AjaxResult getChatGroupData(@RequestBody Map<String, Object> params) {
        List<PcjChatGroup> list = chatGroupService.getChatGroupData(params);
        return AjaxResult.ok(list);
    }

    /**
     * 新增聊天室
     * @param pcjChatGroup
     * @return
     */
    @PostMapping("/addChatGroup")
    public AjaxResult add(@RequestBody PcjChatGroup pcjChatGroup) {
        pcjChatGroup.setUserId(SecurityUtils.getUserId());
        pcjChatGroup.setBeginTime(new Date());
        return AjaxResult.ok(chatGroupService.insertPcjChatGroup(pcjChatGroup));
    }

    /**
     * 获取聊天室默认背景
     * @return
     */
    @PostMapping("/getChatDefBackGround")
    public AjaxResult getChatDefBackGround() {
        return AjaxResult.ok(pcjDefFileConfigService.getDefaultFile("0"));
    }

    /**
     * 进或离开房间
     * @param params
     * @return
     */
    @PostMapping("/comeOrLeaveRoom")
    public AjaxResult comeRoom(@RequestBody Map<String, Object> params) {
        return AjaxResult.ok(chatGroupUserService.savePcjChatUserInfo(params));
    }

    /**
     * 获取在线用户
     */
    @PostMapping("/getOnlineUsers")
    public AjaxResult getOnlineUsers(@RequestBody Map<String, Object> params) {
        return AjaxResult.ok(chatGroupUserService.getOnlineUsers(params));
    }

    /**
     * 销毁房间
     */
    @PostMapping("/destroyRoom")
    public AjaxResult destroyRoom(@RequestBody Map<String, Object> params) {
        return AjaxResult.ok(chatGroupUserService.destroyRoom(params));
    }

    /**
     * 销毁房间
     */
    @PostMapping("/putBlackRoom")
    public AjaxResult putBlackRoom(@RequestBody Map<String, Object> params) {
        return AjaxResult.ok(chatGroupUserService.putBlackRoom(params));
    }


}
